// Copyright 2019 orivil.com. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found at https://mit-license.org.

package actions

import (
	"gitee.com/orivil/morgine/xx"
	"gitee.com/tugoer/psy-testing/code/container"
	"gitee.com/tugoer/psy-testing/code/utils"
)

func GetGameData(c *xx.Condition) {
	doc := xx.Doc{
		Name: "游戏数据服务",
		Params: xx.Params{
			{
				In:     xx.Path,
				Schema: &getGameDataParams{},
			},
		},
	}
	c.Doc(doc).GET().HandleFunc("/game/{id}", func(ctx *xx.Context) {
		p := &getGameDataParams{}
		if ctx.Unmarshal(p) {
			game, err := container.Game.Get(p.ID)
			if err != nil {
				ctx.Error(err)
			} else {
				ctx.SendJson(xx.MAP{"game": game}, false)
			}
		}
	})
}

func GetResultData(c *xx.Condition) {
	doc := xx.Doc{
		Name: "游戏测试结果数据服务",
		Params: xx.Params{
			{
				In:     xx.Query,
				Schema: &getGameResultParams{},
			},
		},
	}
	c.Doc(doc).GET().HandleFunc("/result", func(ctx *xx.Context) {
		p := &getGameResultParams{}
		if ctx.Unmarshal(p) {
			result, err := container.Result.Get(p.ID, p.Option, p.Acid, p.Ext)
			if err != nil {
				ctx.Error(err)
			} else {
				ctx.SendJson(xx.MAP{"result": result}, false)
			}
		}
	})
}

func GetSubjectsData(c *xx.Condition) {
	doc := xx.Doc{
		Name: "获得游戏列表",
		Params: xx.Params{
			{
				In:     xx.Query,
				Schema: &getSubjectsParams{},
			},
		},
	}
	c.Doc(doc).GET().HandleFunc("/subjects", func(ctx *xx.Context) {
		p := &getSubjectsParams{}
		if ctx.Unmarshal(p) {
			subjects, err := container.Subjects.Get(p.Order, p.Page)
			if err != nil {
				ctx.Error(err)
			} else {
				ctx.SendJson(xx.MAP{"subjects": subjects}, false)
			}
		}
	})
}

type getGameDataParams struct {
	ID string `param:"id"`
}

type getGameResultParams struct {
	ID     string `param:"id"`
	Option string `param:"option"`
	Acid   string `param:"acid"`
	Ext    string `param:"ext"`
}

type getSubjectsParams struct {
	Order utils.Order `param:"order" desc:"排序规则: utime-按最近更新排序, view-按热度排序" required:""`
	Page  string      `param:"page" desc:"获取第几页数据" required:""`
}
